﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_SelectUserLeft : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            SectionServices sectionServices=new SectionServices();
            List<Section> list = sectionServices.findAllSection();
            List<Section> baselist = new List<Section>();
            foreach (Section s in list)
            {
                if (s.BaseSection.SectionId==0)
                {
                    baselist.Add(s);
                }
            }
            foreach (Section s in baselist)
            {
                TreeNode tn = new TreeNode(s.SectionName);
                tn.Value = s.SectionId.ToString();
                tn.ImageUrl = @"~/images/Sections.png";
                TreeView1.Nodes.Add(tn);
                AddTreeSonNode(s.SectionId, tn);
            }
            IList<TarreOA.TarreOAModels.User> userList = UserManager.FindAll();
            DataList2.DataSource = userList;
            DataList2.DataBind();
        }
    }
    void AddTreeSonNode(int baseid,TreeNode treeNode)
    {
        List<Section> list = SectionServices.findByBaseSection(baseid);
        if (list.Count>0)
        {
            foreach (Section s in list)
            {
                TreeNode tn = new TreeNode(s.SectionName);
                tn.Value = s.SectionId.ToString();
                tn.ImageUrl = @"~/images/Sections.png";
                treeNode.ChildNodes.Add(tn);
                AddTreeSonNode(s.SectionId,tn);
            }
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        UserManager um=new UserManager();
        List<TarreOA.TarreOAModels.User> list = um.findBySectionId(Convert.ToInt32(TreeView1.SelectedValue));
        DataList2.DataSource = list;
        DataList2.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="getUser")
        {
            List<TarreOA.TarreOAModels.User> list = UserManager.FindByPopedomGroupId(Convert.ToInt32(e.CommandArgument));
            DataList2.DataSource = list;
            DataList2.DataBind();
        }
    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="selectUser")
        {
            TarreOA.TarreOAModels.User user = UserManager.FindByUserId(Convert.ToInt32(e.CommandArgument));
            ClientScript.RegisterStartupScript(this.GetType(),"","var rel=new Array('"+e.CommandArgument.ToString()+"','"+user.UserName+"'); window.returnValue = rel; window.close();",true);
        }
    }
}
